package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.entity.DeviceData;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description DeviceDataServiceImpl
 * @Author dingyanchi
 * @Date 2024-12-29
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    // 查询设备的物模型数据
    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        // PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());
        // //前端传递的时间是一个long类型的毫秒值，作为条件查询数据库需要转换为日期类型
        // Long startTime = deviceDataPageReqDto.getStartTime();
        // Long endTime = deviceDataPageReqDto.getEndTime();
        // deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null : LocalDateTimeUtil.of(startTime));
        // deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null : LocalDateTimeUtil.of(endTime));
        // //查询
        // Page<DeviceData> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);
        // return PageResponse.of(page,DeviceDataVo.class);

        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(deviceDataPageReqDto.getStartDate()) ? null : LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime()));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(deviceDataPageReqDto.getEndDate()) ? null : LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime()));
        Page<DeviceData> deviceData = deviceDataMapper.selectByPage(deviceDataPageReqDto);

        return PageResponse.of(deviceData, DeviceDataVo.class);

    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, Long endTime, String functionId, Long startTime) {
        //按日聚合数据
        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceDataMapper.queryDeviceDataListByDay(iotId, LocalDateTimeUtil.of(startTime),functionId, LocalDateTimeUtil.of(endTime));
        //构建一个24小时的集合
        List<DeviceDataGraphVo> list = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
        //转换为map
        Map<String, Double> map = deviceDataGraphVoList.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        list.forEach(d -> {
            //从map获取值，如果为空，则给默认为0
            Double val = map.get(d.getDateTime()) == null ? 0.0 : map.get(d.getDateTime());
            d.setDataValue(val);
        });
        return list;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, Long endTime, String functionId, Long startTime) {
        //按周聚合数据
        List<DeviceDataGraphVo> listByWeek = deviceDataMapper.queryDeviceDataListByWeek(iotId,LocalDateTimeUtil.of(endTime),functionId,LocalDateTimeUtil.of(startTime));
        //构建一个7天的集合
        List<DeviceDataGraphVo> list = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));
        //转换为map
        Map<String, Double> collect = listByWeek.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        list.forEach(d ->{
            //从map获取值
            Double val = collect.get(d.getDateTime()) == null ? 0.0 : collect.get(d.getDateTime());
            d.setDataValue(val);
        });
        return list;
    }
}
